/*
 * @Description:排序
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2022-12-17 13:41:46
 * @LastEditors: swc shanwenchao12138@163.com
 * @LastEditTime: 2024-04-23 14:36:14
 */
import { ifs, Tsrv, tvs } from '../config';
import { T_bgdBase } from './base';

interface I_filterSort {
  [key: string]: { value: 'asc' | 'desc'; weight: number };
}
export class T_bgdSort extends T_bgdBase {
  protected sort: I_filterSort;

  protected sortArr: Array<ifs.I_sort>;

  constructor(obj: any) {
    super(obj);
    this.sort = {};
    this.sortArr = [];
  }

  public getSortArr(): ifs.I_sort[] {
    return this.sortArr as ifs.I_sort[];
  }

  public getSort(): I_filterSort {
    return this.sort;
  }

  public setSort(p: I_filterSort): T_bgdSort {
    this.sort = p;
    return this;
  }

  public setSortArr(...p: ifs.I_sort[]): T_bgdSort {
    this.sortArr.push(...p);
    return this;
  }

  /**
   * 单列排序
   */
  public addSort(field: string, value: 'asc' | 'desc', weight?: number) {
    const o = this.sort[field];
    if (o) {
      if (o.value === value || value === null) {
        delete this.sort[field];
      } else {
        this.sort[field] = {
          value,
          weight: !weight ? weight : this.sortArr.length
        };
      }
    } else {
      this.sort[field] = {
        value,
        // weight: !weight ? weight : this.sortArr.length
        weight: weight || this.sortArr.length
      };
    }

    this.sortArr = [];
    for (const key in this.sort) {
      this.sortArr.push({
        field: key,
        value: this.sort[key].value as tvs.V_sortValue,
        weight: this.sort[key].weight
      } as ifs.I_sort);
    }
    this.sortArr = Tsrv.utils.sortBy(this.sortArr, 'weight').reverse();
  }

  /**
   * 排序方法
   */
  // 改变排序条件
  public sortChange(e: any) {
    const field = e.field;
    const order = e.order;

    this.addSort(field, order);

    if (this.sortArr.length === 0) {
      this.sortArr.push({
        field: '_sort_',
        value: tvs.V_sortValue.ASC,
        weight: 0
      } as ifs.I_sort);
    }
  }

  /**
   * 清除排序
   */
  public clearSort() {
    this.grid.clearSort();
    this.sort = {};
    this.sortArr = [];
  }

  /**
   * 设置sortID，排序
  
   */

  /**
   * @description: 设置排序行号
   * @param {null} cmd -1最后一行，null第一行，row行之下
   * @return {*}
   * @author: wiz
   * @Date: 2022-12-27 15:12:43
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */

  public updateSortNum(cmd: null | number) {
    // let sortID = 0;
    // const data = this.grid.getTableData().visibleData;
    // data.forEach(el => {
    //   if (el.id === null) {
    //     sortID += 0.000001;
    //     el._sort_ = sortID;
    //   } else {
    //     if (el._sort_ && el._sort_ !== null) {
    //       sortID = el._sort_;
    //     }
    //   }
    // });
    let num: number = 0;
    const data = this.grid.getTableData().visibleData;
    data.forEach(el => {
      // -1 null 新建行和最后一行后端会处理
      if (cmd && cmd > -1) {
        num += 0.000001;
        el['_sort_'] = num;
      }
    });
  }
}

export interface I_bgdSort extends T_bgdSort {}
